package com.wx.clazzloader.hotreload;

import cn.hutool.core.io.file.FileReader;

import java.io.File;

/**
 * 自定义类加载器，只加载当前目录下的类
 * @description:
 * @author: wangxun
 * @createDate: 2021/12/28
 * @version: 1.0
 */
public class ReLoadClassLoader extends ClassLoader {

    /**后缀*/
    public static final String CLASS_FILE_SUFFIX  = ".class";

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {


            // 前置路径
            String classPath = "E:\\clazz\\";
            // 类文件
            File classFile = new File(classPath + name + CLASS_FILE_SUFFIX);

            FileReader fileReader = FileReader.create(classFile);
            byte[] bytes = fileReader.readBytes();

            return defineClass(name,bytes,0,bytes.length);



    }
}
